package com.ztr.test.ip;

import java.net.InetAddress;
import java.net.NetworkInterface;  
import java.net.SocketException;  
import java.util.Enumeration;  
  
public class SpecificLocalIPAddress {  
    public static void main(String[] args) {  
        String interfaceName = "wlan2"; // 设置要查找的接口名称  
        try {  
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();  
            while (interfaces.hasMoreElements()) {  
                NetworkInterface into = interfaces.nextElement();
                if (into.getName().equals(interfaceName)) { // 检查接口名称是否匹配
                    for (Enumeration<InetAddress> adders = into.getInetAddresses(); adders.hasMoreElements();) {
                        InetAddress addr = adders.nextElement();
                        if (!addr.isLoopbackAddress()) { // 忽略回环地址（例如127.0.0.1）  
                            System.out.println("接口名称: " + into.getName() + " IP地址: " + addr.getHostAddress());
                        }  
                    }  
                }  
            }  
        } catch (SocketException e) {  
            e.printStackTrace();  
        }  
    }  
}